package ru.alexbykov.nopaginate.paginate;

import ohos.agp.database.DataSetSubscriber;

import ru.alexbykov.nopaginate.callback.OnAdapterChangeListener;

/**
 * @author @Marko Milos, original repository: https://github.com/MarkoMilos/Paginate
 */
final class WrapperAdapterObserver extends DataSetSubscriber {
    private WrapperAdapter wrapperAdapter;
    private OnAdapterChangeListener adapterChangeListener;

    WrapperAdapterObserver(OnAdapterChangeListener adapterChangeListener, WrapperAdapter wrapperAdapter) {
        this.wrapperAdapter = wrapperAdapter;
        this.adapterChangeListener = adapterChangeListener;
    }

    @Override
    public void onItemRangeChanged(int startPos, int countItems) {
        wrapperAdapter.notifyDataSetItemRangeChanged(startPos, countItems);
        adapterChangeListener.onAdapterChange();
    }

    @Override
    public void onItemRangeInserted(int startPos, int countItems) {
        wrapperAdapter.notifyDataSetItemRangeChanged(startPos, countItems);
        adapterChangeListener.onAdapterChange();
    }

    @Override
    public void onItemRangeRemoved(int startPos, int countItems) {
        wrapperAdapter.notifyDataSetItemRangeRemoved(startPos, countItems);
        adapterChangeListener.onAdapterChange();
    }

    @Override
    public void onChanged() {
        wrapperAdapter.notifyDataChanged();
        adapterChangeListener.onAdapterChange();
    }
}
